-- TransferAction
-- create by zouyb
-- 格挡动作（伤害转移）

TransferAction = class("TransferAction", function()
    return ActionBase.create();
end);
function TransferAction.create(source, target, oriTarget)
    local self = TransferAction.new();
    self.source = source;
    self.target = target;
    self.oriTarget = oriTarget;
    return self;
end

-- 构造函数
function TransferAction:ctor()
end

-- 开始播放
function TransferAction:play()
    local fromPos = UIDungeonMgr:getCurLevel():getMonsterPos(self.oriTarget:getOwner());
    local toPos = UIDungeonMgr:getCurLevel():getMonsterPos(self.target:getOwner());
    local fromNode = UIDungeonMgr:getCurLevel():getAllGrids()[self.oriTarget:getOwner()];
    local toNode = UIDungeonMgr:getCurLevel():getAllGrids()[self.target:getOwner()];
    local heroPos = UIDungeonMgr:getCurLevel():getHeroPos();

    -- 稍微向上平移一些
    fromPos.y = fromPos.y + 30;
    toPos.y = toPos.y + 30;

    -- 创建射线轨迹
    -- 计算两点间的距离
    local distance = cc.pGetDistance(fromPos, toPos);

    -- 计算水平射线需要旋转的角度
    -- 由于setRotation是顺时针旋转的，因此需要取反
    local angle = - calcPointAngle(fromPos, toPos);
    local angle2 = -calcPointAngle(heroPos, fromPos);

    -- 射线光效的长度
    local unitLength = 642;

    local scaleX = distance / unitLength;

    local node = cc.Node:create();
    local node2 = cc.Node:create();
    local effectNode = UIDungeonMgr:getCurLevel():getEffectParent();
    node:setScaleX(scaleX);
    -- node2:setScaleX(scaleX);
    node:setPosition(cc.p(fromPos.x, fromPos.y));  -- 修正一下射线光效的起始位置
    node2:setPosition(cc.p(fromPos.x, fromPos.y));  -- 修正一下射线光效的起始位置
    node:setRotation(angle);
    node2:setRotation(angle2 + 180);
    effectNode:addChild(node);
    effectNode:addChild(node2);

    local x = (toPos.x - fromPos.x) / 3;
    local y = (toPos.y - fromPos.y) / 3;

    local function effectFun1()
        -- 播放原怪物盾牌光效
        AudioM.playFx("knife_block");
        playEffect(node2, 1225);
        playEffect(fromNode, 1224, 0, 20);
    end

    local function effectFun2()
        local function frameEventFunc(frame)
            if frame.getEvent and frame:getEvent() == "pilor" then
                local name = frame:getNode():getName();
                local index = frame:getFrameIndex();
                if index >= 4 then
                   node:setPositionX(node:getPositionX() + x);
                   node:setPositionY(node:getPositionY() + y);
                end
                if index == 6 then
                   node:setVisible(false);
                end
            end
        end
        -- 射线光效
        playEffect(node, 1226, 0, 0,frameEventFunc);
    end

    local function effectFun3()
        -- 格挡怪物光亮
        playEffect(toNode, 1227, 0, 15);
    end

    local function effectFun4()
        -- 去掉节点
        effectNode:removeChild(node);
        effectNode:removeChild(node2);
    end

    local callfunc1 = cc.CallFunc:create(effectFun1);
    local callfunc2 = cc.CallFunc:create(effectFun2);
    local callfunc3 = cc.CallFunc:create(effectFun3);
    local callfunc4 = cc.CallFunc:create(effectFun4);
    local delay1 = cc.DelayTime:create(0.2);
    local delay2 = cc.DelayTime:create(1.5);

    effectNode:runAction(cc.Sequence:create(callfunc1, delay1, callfunc2, delay1, callfunc3, delay2, callfunc4));
end

return TransferAction;